
package com.v1.video.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.iss.view.common.ToastAlone;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.domain.MyloginVcodeOfForgetEntity;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.ParamList;
import com.v1.video.httpmanager.ParamList.Parameter;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.pay.MD5;
import com.v1.video.util.Logger;
import com.v1.video.util.Utility;
import com.v1.video.util.Utils;
import com.v1.video.widgets.MyDialog;

public class SettingForget3Activity extends BaseActivity implements OnClickListener {
//    private EditText editText_phone;
    private EditText editText_password;
    private EditText editText_password_new; 
    private TextView txtTitle;
    private Button btnSure;
    private final int STATE_SEND_PWD_SUCCESS = 0X00001;
    private final int STATE_SEND_PWD_FAIL = 0X00002;
    private String key = "";
    private String phoneNum = "";
    private ImageView imgIconPwd = null;
    private ImageView imgIconPwdSure = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_login_forget3);
    }

    @Override
    protected void initView() {
//        editText_phone = (EditText)findViewById(R.id.editText_phone);
        editText_password = (EditText)findViewById(R.id.editText_password);
        editText_password_new = (EditText)findViewById(R.id.editText_pwd_sure);
        txtTitle = (TextView)findViewById(R.id.tv_title);
        btnSure = (Button)findViewById(R.id.forget_btn_sure);
        imgIconPwd = (ImageView)findViewById(R.id.forget3_icon_input);
        imgIconPwdSure = (ImageView)findViewById(R.id.forget3_icon_input_sure);
    }

    @Override
    protected void initData() {
    	txtTitle.setText(getResources().getString(R.string.setting_login_forget));
    	btnSure.setEnabled(false);
    	Intent intent = getIntent();
		phoneNum = intent.getStringExtra("phoneNum");
		key =  intent.getStringExtra("key");
		imgIconPwdSure.setEnabled(false);
//    	((TextView)findViewById(R.id.tv_right)).setText("下一 步");
    }

    @Override
    protected void setListener() {
//        findViewById(R.id.tv_right).setVisibility(View.VISIBLE);
        findViewById(R.id.forget_btn_sure).setOnClickListener(this);
        findViewById(R.id.lay_result).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        editText_password.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(hasFocus){
					imgIconPwd.setEnabled(true);
				}else{
					imgIconPwd.setEnabled(false);
				}
			}
		});
        editText_password_new.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(hasFocus){
					imgIconPwdSure.setEnabled(true);
				}else{
					imgIconPwdSure.setEnabled(false);
				}
			}
		});
        editText_password.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				int length = editText_password.getText().toString().length();
				int length1 = editText_password_new.getText().toString().length();
				if(length <= 0 || length1 <= 0){
					btnSure.setEnabled(false);
				}else{
					btnSure.setEnabled(true);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
        editText_password_new.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				int length = editText_password.getText().toString().length();
				int length1 = editText_password_new.getText().toString().length();
				if(length <= 0 || length1 <= 0){
					btnSure.setEnabled(false);
				}else{
					btnSure.setEnabled(true);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forget_btn_sure:
            case R.id.tv_right:
            	
            	String password = editText_password.getText().toString();
            	if(password == null || password.equals("")){
    				Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
    				return;
    			}
            	
            	String passwordNew = editText_password_new.getText().toString();
            	if(passwordNew == null || passwordNew.equals("")){
    				Toast.makeText(this, "请再次输入新密码", Toast.LENGTH_SHORT).show();
    				return;
    			}
            	
            	if(!Utility.isConnected(SettingForget3Activity.this)){
	    			Toast.makeText(this, "无网络连接，请检查网络设置", Toast.LENGTH_SHORT).show();
					return;
	    		}
            	
            	if(passwordNew.equals(password)){
            		sendKey(phoneNum,key,password);
            	}else{
            		Toast.makeText(this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
            	}
                break;
        }

    }
    
	private ProgressDialog progDialog;
	
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case STATE_SEND_PWD_SUCCESS:
				if (progDialog != null) {
					progDialog.dismiss();
				}
				progDialog = null;
				
				createDialog();
				break;
			case STATE_SEND_PWD_FAIL:
				if (progDialog != null) {
					progDialog.dismiss();
				}
				progDialog = null;
				break;
			default:
				break;
			}
		}		
	};
	
	/**
	 * 修改密码：发送key
	 * @param phoneNum
	 * @param vcode
	 */
	private void sendKey(final String phoneNum, final String cdkey,final String password) {
		progDialog = Utils.getProgressDialog(SettingForget3Activity.this,"请稍候");
//		progDialog = ProgressDialog.show(this, null, "请稍候", true, false);
		String key = Constant.getUserCheckKey(phoneNum);
		key = MD5.getMessageDigest(key);
		String url = V1VideoHttpApi.GET_MODIFY_OF_PWD_FORGET() + "&devid="
				+ Constant.DEVICEID + "&key=" + key;
		
		Logger.i(TAG, "忘记密码的：带验证 码修改密码Url =" + url);
		ParamList list = new ParamList();
		list.add(new Parameter("username", phoneNum));
		list.add(new Parameter("cdkey", cdkey));
		list.add(new Parameter("password", password));
		RequestManager.getInstance().postRequest(SettingForget3Activity.this, url, list,
				MyloginVcodeOfForgetEntity.class, 0, "SettingForget2Activity",
				new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String key) {
						// TODO Auto-generated method stub
						Logger.i(TAG, "onSuccess.object=" + object.toString());
						if(progDialog != null){
							progDialog.dismiss();
						}
						progDialog = null;
						if (object != null) {
							MyloginVcodeOfForgetEntity entity = (MyloginVcodeOfForgetEntity) object;
							com.v1.video.domain.BaseEntity.Header header = entity
									.getHeader();
							if (header == null) {
								// ToastAlone.showToast(SettingForget1Activity.this,
								// "未知错误", Toast.LENGTH_SHORT);
								return;
							} else if (!header.getStatus().equals("1")) {

								// ToastAlone.showToast(SettingForget1Activity.this,
								// header.getMessage(), Toast.LENGTH_SHORT);
								return;
							} else if (header.getStatus().equals("1")) {
								if (entity.getBody() != null) {
									if (entity.getBody().getData().getStatus()
											.equals("1")) {
										if (mHandler != null) {
											Message msg = mHandler
													.obtainMessage();
											msg.what = STATE_SEND_PWD_SUCCESS;
											msg.obj = entity.getBody().getData().getKey();
											mHandler.sendMessage(msg);
										}

									} else {
										ToastAlone.showToast(SettingForget3Activity.this, entity
												.getBody().getData()
												.getMessage(),
												Toast.LENGTH_SHORT);
									}

								} else {
									// toast(header.getMessage());
								}
							} else {
							}
						}

					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						Logger.i(TAG, message);
						if(progDialog != null){
							progDialog.dismiss();
						}
						progDialog = null;
					}
				});
	}
	
	private void createDialog() {

		final MyDialog dialog = Utility.createMyDialog(this,
				R.style.dialog_custom, R.layout.dialog_title_done);
		((TextView) dialog.findViewById(R.id.tv_dialog_title))
				.setText("密码修改成功");
		((TextView) dialog.findViewById(R.id.tv_dialog_content))
				.setText("下次登录请使用新密码");
		TextView ok = (TextView) dialog.findViewById(R.id.tv_dialog_ok);
		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				finish();
			}
		});
		dialog.show();

	}
}

